package com.bng8.superstar_cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bng8.superstar_cms.domain.common.PageQuery;
import com.bng8.superstar_cms.domain.common.PageResult;
import com.bng8.superstar_cms.domain.dto.SupMissionDTO;
import com.bng8.superstar_cms.domain.dto.SupMissionQueryDTO;
import com.bng8.superstar_cms.domain.entity.SupMissionDO;
import com.bng8.superstar_cms.mapper.SupMissionMapper;
import com.bng8.superstar_cms.service.SupMissionService;
import com.bng8.superstar_cms.util.ValidatorUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 20393
 * @description 针对表【sup_mission(任务明细表)】的数据库操作Service实现
 * @createDate 2023-08-17 11:22:19
 */
@Service
public class SupMissionServiceImpl extends ServiceImpl<SupMissionMapper, SupMissionDO>
        implements SupMissionService {

    @Autowired
    private SupMissionMapper missionMapper;

    private SupMissionDO missionDO = new SupMissionDO();

    /**
     * 分页查询任务信息
     *
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult<List<SupMissionDTO>> query(PageQuery<SupMissionQueryDTO> pageQuery) {
        // 手动校验功能
        ValidatorUtils.validate(pageQuery);

        // 参数构造
        Page page = new Page(pageQuery.getPageNo(), pageQuery.getPageSize());

        BeanUtils.copyProperties(pageQuery.getQuery(), missionDO);

        // TODO 如果属性不一致，需要做特殊处理
        QueryWrapper<SupMissionDO> queryWrapper =
                new QueryWrapper<SupMissionDO>()
                        .like("user_name", missionDO.getUserName());

        // 查询
        IPage<SupMissionDO> missionDOIPage = missionMapper.selectPage(page, queryWrapper);

        // 结果解析
        PageResult pageResult = new PageResult();
        pageResult.setPageNo((int) missionDOIPage.getCurrent());
        pageResult.setPageSize((int) missionDOIPage.getSize());
        pageResult.setTotal(missionDOIPage.getTotal());
        pageResult.setPageNum(missionDOIPage.getPages());

        // 对数据进行转换
        List<SupMissionDTO> missionDTOList = Optional
                .ofNullable(missionDOIPage.getRecords())
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(missionDO -> {
                    SupMissionDTO supMissionDTO = new SupMissionDTO();
                    BeanUtils.copyProperties(missionDO, supMissionDTO);
                    return supMissionDTO;
                })
                .collect(Collectors.toList());

        pageResult.setData(missionDTOList);

        return pageResult;
    }

    /**
     * 新增任务信息
     *
     * @param missionDTO
     * @return
     */
    @Override
    public int save(SupMissionDTO missionDTO) {
        BeanUtils.copyProperties(missionDTO, missionDO);
        return missionMapper.insert(missionDO);
    }

    /**
     * 更新任务信息
     *
     * @param id
     * @param missionDTO
     * @return
     */
    @Override
    public int update(String id, SupMissionDTO missionDTO) {
        missionDTO.setId(id);
        BeanUtils.copyProperties(missionDTO, missionDO);
        return missionMapper.updateById(missionDO);
    }

    @Override
    public int deleteById(String id) {
        missionDO = missionMapper.selectById(id);
        if (missionDO != null) {
            return missionMapper.deleteById(missionDO);
        }
        return 1;
    }
}




